home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / bigfloat.pl < prev    next >
Encoding:
Perl Script  |  1999-12-28  |  4.9 KB  |  189 lines

  1. package bigfloat;
  2. require "bigint.pl";
  3.  
  4. $div_scale = 40;
  5.  
  6.  
  7. $rnd_mode = 'even';
  8.  
  9.  
  10. sub main'fnorm { #(string) return fnum_str
  11.     local($_) = @_;
  12.     s/\s+//g;                               # strip white space
  13.     if (/^([+-]?)(\d*)(\.(\d*))?([Ee]([+-]?\d+))?$/
  14.       && ($2 ne '' || defined($4))) {
  15.     my $x = defined($4) ? $4 : '';
  16.     &norm(($1 ? "$1$2$x" : "+$2$x"), (($x ne '') ? $6-length($x) : $6));
  17.     } else {
  18.     'NaN';
  19.     }
  20. }
  21.  
  22. sub norm { #(mantissa, exponent) return fnum_str
  23.     local($_, $exp) = @_;
  24.     if ($_ eq 'NaN') {
  25.     'NaN';
  26.     } else {
  27.     s/^([+-])0+/$1/;                        # strip leading zeros
  28.     if (length($_) == 1) {
  29.         '+0E+0';
  30.     } else {
  31.         $exp += length($1) if (s/(0+)$//);  # strip trailing zeros
  32.         sprintf("%sE%+ld", $_, $exp);
  33.     }
  34.     }
  35. }
  36.  
  37. sub main'fneg { #(fnum_str) return fnum_str
  38.     local($_) = &'fnorm($_[$[]);
  39.     vec($_,0,8) ^= ord('+') ^ ord('-') unless $_ eq '+0E+0'; # flip sign
  40.     s/^H/N/;
  41.     $_;
  42. }
  43.  
  44. sub main'fabs { #(fnum_str) return fnum_str
  45.     local($_) = &'fnorm($_[$[]);
  46.     s/^-/+/;                               # mash sign
  47.     $_;
  48. }
  49.  
  50. sub main'fmul { #(fnum_str, fnum_str) return fnum_str
  51.     local($x,$y) = (&'fnorm($_[$[]),&'fnorm($_[$[+1]));
  52.     if ($x eq 'NaN' || $y eq 'NaN') {
  53.     'NaN';
  54.     } else {
  55.     local($xm,$xe) = split('E',$x);
  56.     local($ym,$ye) = split('E',$y);
  57.     &norm(&'bmul($xm,$ym),$xe+$ye);
  58.     }
  59. }
  60.  
  61. sub main'fadd { #(fnum_str, fnum_str) return fnum_str
  62.     local($x,$y) = (&'fnorm($_[$[]),&'fnorm($_[$[+1]));
  63.     if ($x eq 'NaN' || $y eq 'NaN') {
  64.     'NaN';
  65.     } else {
  66.     local($xm,$xe) = split('E',$x);
  67.     local($ym,$ye) = split('E',$y);
  68.     ($xm,$xe,$ym,$ye) = ($ym,$ye,$xm,$xe) if ($xe < $ye);
  69.     &norm(&'badd($ym,$xm.('0' x ($xe-$ye))),$ye);
  70.     }
  71. }
  72.  
  73. sub main'fsub { #(fnum_str, fnum_str) return fnum_str
  74.     &'fadd($_[$[],&'fneg($_[$[+1]));    
  75. }
  76.  
  77. sub main'fdiv #(fnum_str, fnum_str[,scale]) return fnum_str
  78. {
  79.     local($x,$y,$scale) = (&'fnorm($_[$[]),&'fnorm($_[$[+1]),$_[$[+2]);
  80.     if ($x eq 'NaN' || $y eq 'NaN' || $y eq '+0E+0') {
  81.     'NaN';
  82.     } else {
  83.     local($xm,$xe) = split('E',$x);
  84.     local($ym,$ye) = split('E',$y);
  85.     $scale = $div_scale if (!$scale);
  86.     $scale = length($xm)-1 if (length($xm)-1 > $scale);
  87.     $scale = length($ym)-1 if (length($ym)-1 > $scale);
  88.     $scale = $scale + length($ym) - length($xm);
  89.     &norm(&round(&'bdiv($xm.('0' x $scale),$ym),$ym),
  90.         $xe-$ye-$scale);
  91.     }
  92. }
  93.  
  94. sub round { #(int_str, int_str, int_str) return int_str
  95.     local($q,$r,$base) = @_;
  96.     if ($q eq 'NaN' || $r eq 'NaN') {
  97.     'NaN';
  98.     } elsif ($rnd_mode eq 'trunc') {
  99.     $q;                         # just truncate
  100.     } else {
  101.     local($cmp) = &'bcmp(&'bmul($r,'+2'),$base);
  102.     if ( $cmp < 0 ||
  103.          ($cmp == 0 &&
  104.           ( $rnd_mode eq 'zero'                             ||
  105.            ($rnd_mode eq '-inf' && (substr($q,$[,1) eq '+')) ||
  106.            ($rnd_mode eq '+inf' && (substr($q,$[,1) eq '-')) ||
  107.            ($rnd_mode eq 'even' && $q =~ /[24680]$/)        ||
  108.            ($rnd_mode eq 'odd'  && $q =~ /[13579]$/)        )) ) {
  109.         $q;                     # round down
  110.     } else {
  111.         &'badd($q, ((substr($q,$[,1) eq '-') ? '-1' : '+1'));
  112.     }
  113.     }
  114. }
  115.  
  116. sub main'fround { #(fnum_str, scale) return fnum_str
  117.     local($x,$scale) = (&'fnorm($_[$[]),$_[$[+1]);
  118.     if ($x eq 'NaN' || $scale <= 0) {
  119.     $x;
  120.     } else {
  121.     local($xm,$xe) = split('E',$x);
  122.     if (length($xm)-1 <= $scale) {
  123.         $x;
  124.     } else {
  125.         &norm(&round(substr($xm,$[,$scale+1),
  126.              "+0".substr($xm,$[+$scale+1,1),"+10"),
  127.           $xe+length($xm)-$scale-1);
  128.     }
  129.     }
  130. }
  131.  
  132. sub main'ffround { #(fnum_str, scale) return fnum_str
  133.     local($x,$scale) = (&'fnorm($_[$[]),$_[$[+1]);
  134.     if ($x eq 'NaN') {
  135.     'NaN';
  136.     } else {
  137.     local($xm,$xe) = split('E',$x);
  138.     if ($xe >= $scale) {
  139.         $x;
  140.     } else {
  141.         $xe = length($xm)+$xe-$scale;
  142.         if ($xe < 1) {
  143.         '+0E+0';
  144.         } elsif ($xe == 1) {
  145.         &norm(&round('+0',"+0".substr($xm,$[+1,1),"+10"), $scale);
  146.         } else {
  147.         &norm(&round(substr($xm,$[,$xe),
  148.               "+0".substr($xm,$[+$xe,1),"+10"), $scale);
  149.         }
  150.     }
  151.     }
  152. }
  153.     
  154. sub main'fcmp #(fnum_str, fnum_str) return cond_code
  155. {
  156.     local($x, $y) = (&'fnorm($_[$[]),&'fnorm($_[$[+1]));
  157.     if ($x eq "NaN" || $y eq "NaN") {
  158.     undef;
  159.     } else {
  160.     ord($y) <=> ord($x)
  161.     ||
  162.     (  local($xm,$xe,$ym,$ye) = split('E', $x."E$y"),
  163.          (($xe <=> $ye) * (substr($x,$[,1).'1')
  164.              || &bigint'cmp($xm,$ym))
  165.     );
  166.     }
  167. }
  168.  
  169. sub main'fsqrt { #(fnum_str[, scale]) return fnum_str
  170.     local($x, $scale) = (&'fnorm($_[$[]), $_[$[+1]);
  171.     if ($x eq 'NaN' || $x =~ /^-/) {
  172.     'NaN';
  173.     } elsif ($x eq '+0E+0') {
  174.     '+0E+0';
  175.     } else {
  176.     local($xm, $xe) = split('E',$x);
  177.     $scale = $div_scale if (!$scale);
  178.     $scale = length($xm)-1 if ($scale < length($xm)-1);
  179.     local($gs, $guess) = (1, sprintf("1E%+d", (length($xm)+$xe-1)/2));
  180.     while ($gs < 2*$scale) {
  181.         $guess = &'fmul(&'fadd($guess,&'fdiv($x,$guess,$gs*2)),".5");
  182.         $gs *= 2;
  183.     }
  184.     &'fround($guess, $scale);
  185.     }
  186. }
  187.  
  188. 1;
  189.